package com.yicong.ants.wxapi;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxBus;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.manager.PayManager;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.mvp.base.BaseActivity;
import com.yicong.ants.ui.main.MainConfigActivity;
import com.yicong.ants.ui.order.BillHistoryTabActivity;
import com.yicong.ants.utils.L;
import com.yicong.ants.view.CustomToolBar;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    ImageView ivPayResult;
    TextView tvPayResult;
    TextView tvCheckAnOrder;
    TextView tvBackHomepage;
    CustomToolBar payToolBar;
    private IWXAPI api;

    @Override
    protected int getContentLayoutRes() {
        return R.layout.pay_result;
    }

    @Override
    protected void initDatas() {
        if (null != getIntent() && getIntent().hasExtra(Constants.Extra.Code)) {
            showSatateView(getIntent().getIntExtra(Constants.Extra.Code, 1));
        }
        api = WXAPIFactory.createWXAPI(this, Constants.WeChatId);
        api.handleIntent(getIntent(), this);

        if (PayManager.CUR_PAY_CATEGORY.equals("card")) {
            ((TextView) (findViewById(R.id.tv_check_an_order))).setText("查看卡信息");
        }
    }

    @Override
    protected void initViews() {
        ivPayResult = findViewById(R.id.iv_pay_result);
        tvPayResult = findViewById(R.id.tv_pay_result);
        tvCheckAnOrder = findViewById(R.id.tv_check_an_order);
        tvBackHomepage = findViewById(R.id.tv_back_homepage);
        payToolBar = findViewById(R.id.pay_tool_bar);

        payToolBar.setCallBack(() -> finish());

        View.OnClickListener listener = v -> onViewClicked(v);
        tvCheckAnOrder.setOnClickListener(listener);
        tvBackHomepage.setOnClickListener(listener);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        L.e("onResp", "onPayFinish, errCode = " + resp.errCode
            + "   WeChatId   " + Constants.WeChatId
            + "   type   " + resp.getType());
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            showSatateView(resp.errCode);
        }
    }

    /**
     * 更新界面
     *
     * @param code 0=suc   1=fail
     */
    private void showSatateView(int code) {
        if ("rp_verify".equals(PayManager.CUR_PAY_CATEGORY)) {
            if (code == 0) {
                RxBus.get().postEvent(Constants.Event.Pay_Result_Suc);
            } else {
                RxBus.get().postEvent(Constants.Event.Pay_Result_Fail);
            }
            finish();
            return;
        }
        if (code == 0) {
            ivPayResult.setBackgroundResource(R.drawable.icon_payment_success);
            tvPayResult.setText(getResources().getString(R.string.payment_success));
        } else {
            ivPayResult.setBackgroundResource(R.drawable.icon_payment_failure);
            tvPayResult.setText(getResources().getString(R.string.payment_failure));
        }
    }

    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_check_an_order:
                switch (PayManager.CUR_PAY_CATEGORY) {
                    case "ticket":
                        Router.turnTo(mContext, BillHistoryTabActivity.class)
                            .putExtra(Constants.Extra.Index, 0)
                            .checkLogin(true).start();
                        break;
                    case "addedProject":
                        Router.turnTo(mContext, BillHistoryTabActivity.class)
                            .putExtra(Constants.Extra.Index, 1)
                            .checkLogin(true).start();
                        break;
                }
                RxBus.get().postEvent(Constants.Event.Pay_Result_Turn);
                finish();
                break;
            case R.id.tv_back_homepage:
                Router.turnTo(mContext, MainConfigActivity.class).start();
                if ("card".equals(PayManager.CUR_PAY_CATEGORY)) {
                    UserManager.refreshUserInfo();
                }
                break;
        }
        PayManager.CUR_PAY_CATEGORY = "null";
    }
}
